package net.linku.utils;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

/**
 * @ClassName RandomAvatarGeneratorUtil
 * @Description: 随机头像生成类
 * @Author SilenceTian
 * @Date 2024/1/2
 * @Version V1.0.2
 **/
@Slf4j
public class RandomAvatarGeneratorUtil {

    @Autowired
    private OSS ossClient ;

    @Value("${oss.bucket.name:linku-imgs}")
    private String bucketName;



    public static void main(String[] args) {
        generateAndUploadAvatar("Zhangsan", DateUtil.today().replaceAll("-", "/") + "/" + "ProfilePhoto" + "nickName");
    }

    public static String generateAndUploadAvatar(String username, String fileName) {
        try {
            // 创建一个新的 BufferedImage
            int width = 200;
            int height = 200;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = image.createGraphics();

            // 生成随机颜色
            Random rand = new Random();
            Color randomColor = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));

            // 绘制随机背景
            g2d.setColor(randomColor);
            g2d.fillRect(0, 0, width, height);

            // 设置字体
            Font font = new Font("Arial", Font.BOLD, 20);
            g2d.setFont(font);

            // 设置文字颜色
            g2d.setColor(Color.WHITE);

            // 居中绘制用户名
            FontMetrics fm = g2d.getFontMetrics();
            int textWidth = fm.stringWidth(username);
            int textHeight = fm.getHeight();
            int x = (width - textWidth) / 2;
            int y = (height - textHeight) / 2 + fm.getAscent();
            g2d.drawString(username, x, y);

            // 释放资源
            g2d.dispose();

            log.info("Random avatar with username generated and uploaded to OSS");
            // 将生成的头像上传到 OSS
            return uploadToOSS(image, fileName, "linku-imgs");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return username;
    }

    private static String uploadToOSS(BufferedImage image, String fileName, String bucketName) throws IOException {
        // 请填写你的 OSS Endpoint、AccessKeyId 和 AccessKeySecret
        String endPoint = "oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t8oKigT7THEwPsHUmGk";
        String accessKeySecret = "dt6gzetUmp4gDVT1j6Rh7Haph9RWXa";

        // 创建 OSS 客户端
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        try {
            // 将 BufferedImage 转换为输入流
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(image, "png", os);
            InputStream is = new ByteArrayInputStream(os.toByteArray());

            // 设置对象元信息，例如 Content-Type
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("image/png");

            // 上传头像到 OSS
            ossClient.putObject(bucketName, fileName, is, metadata);
            return "https://" + bucketName + "." + endPoint + "/" + fileName;
        } finally {
            // 关闭 OSS 客户端
            ossClient.shutdown();
        }

    }
}

